﻿using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Configuration;
using System.Globalization;
using System.Reflection;

namespace System.Runtime.Serialization
{
	internal static class SerializationInfoExtension
	{
		internal static T GetValue<T>(this SerializationInfo info, string name)
		{
			return (T)info.GetValue(name, typeof(T));
		}
	}
}

namespace System.Windows.Forms
{
	public class ColumnHeaderExConverter : ExpandableObjectConverter
	{
		// Methods
		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context, destinationType));
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			ConstructorInfo constructor;
			if (destinationType == null)
			{
				throw new ArgumentNullException("destinationType");
			}
			if (!(destinationType == typeof(InstanceDescriptor)) || !(value is ColumnHeaderEx))
			{
				return base.ConvertTo(context, culture, value, destinationType);
			}
			ColumnHeaderEx header = (ColumnHeaderEx)value;
			Type reflectionType = TypeDescriptor.GetReflectionType(value);
			InstanceDescriptor descriptor = null;
			if (header.ImageIndex != -1)
			{
				constructor = reflectionType.GetConstructor(new Type[] { typeof(int) });
				if (constructor != null)
				{
					descriptor = new InstanceDescriptor(constructor, new object[] { header.ImageIndex }, false);
				}
			}
			if ((descriptor == null) && !string.IsNullOrEmpty(header.ImageKey))
			{
				constructor = reflectionType.GetConstructor(new Type[] { typeof(string) });
				if (constructor != null)
				{
					descriptor = new InstanceDescriptor(constructor, new object[] { header.ImageKey }, false);
				}
			}
			if (descriptor != null)
			{
				return descriptor;
			}
			constructor = reflectionType.GetConstructor(Type.EmptyTypes);
			if (constructor == null)
			{
				throw new ArgumentException("NoDefaultConstructor");
			}
			return new InstanceDescriptor(constructor, new object[0], false);
		}
	}

	[TypeConverter(typeof(ColumnHeaderExConverter)), ToolboxItem(false), DesignTimeVisible(false), DefaultProperty("Text")]
	public partial class ColumnHeaderEx : ColumnHeader, ICloneable
	{
		public ColumnHeaderEx()
		{
		}

		public ColumnHeaderEx(int imageIndex) : base(imageIndex)
		{
		}

		public ColumnHeaderEx(string imageKey) : base(imageKey)
		{
		}

		[Localizable(true)]
		public new int DisplayIndex
		{
			get { return base.DisplayIndex; }
			set { base.DisplayIndex = value; }
		}

		[DefaultValue((String)null)]
		public ContextMenuStrip DropDownContextMenu
		{
			get { return Extras.DropDownContextMenu; }
			set { Extras.DropDownContextMenu = value; }
		}

		[DefaultValue((String)null)]
		public string Format
		{
			get { return Extras.Format; }
			set { Extras.Format = value; }
		}

		[DefaultValue(null)]
		public IFormatProvider FormatProvider
		{
			get { return Extras.FormatProvider; }
			set { Extras.FormatProvider = value; }
		}

		[DefaultValue(true)]
		public bool Hideable
		{
			get { return Extras.Hideable; }
			set { Extras.Hideable = value; }
		}

		[DefaultValue((String)null)]
		public string NullValue
		{
			get { return Extras.NullValue; }
			set { Extras.NullValue = value; }
		}

		[DefaultValue(false)]
		public bool ShowDropDownButton
		{
			get { return Extras.ShowDropDownButton; }
			set
			{
				Extras.ShowDropDownButton = value;
				if (base.ListView != null)
					base.ListView.SetColumnDropDown(base.Index, value);
			}
		}

		[DefaultValue(true)]
		public bool Sortable
		{
			get { return Extras.Sortable; }
			set { Extras.Sortable = value; }
		}

		[DefaultValue(0)]
		public SortOrder SortOrder
		{
			get { return Extras.SortOrder; }
			set { Extras.SortOrder = value; }
		}

		[DefaultValue(null)]
		public new object Tag
		{
			get { return Extras.Tag; }
			set { Extras.Tag = value; }
		}

		internal InternalColInfo Extras
		{
			get
			{
				if (base.Tag == null)
					return (InternalColInfo)(base.Tag = new InternalColInfo());
				if (base.Tag is InternalColInfo)
					return (InternalColInfo)base.Tag;
				return (InternalColInfo)(base.Tag = new InternalColInfo() { Tag = base.Tag });
			}
			set
			{
				if (base.Tag != null && !(base.Tag is InternalColInfo))
					value.Tag = base.Tag;
				base.Tag = value;
			}
		}

		public new object Clone()
		{
			ColumnHeaderEx header = new ColumnHeaderEx() { Text = this.Text, TextAlign = this.TextAlign, Width = this.Width };
			header.DropDownContextMenu = this.DropDownContextMenu;
			header.Format = this.Format;
			header.FormatProvider = this.FormatProvider;
			header.Hideable = this.Hideable;
			header.NullValue = this.NullValue;
			header.ShowDropDownButton = this.ShowDropDownButton;
			header.Sortable = this.Sortable;
			header.SortOrder = this.SortOrder;
			header.Tag = (this.Tag is ICloneable) ? ((ICloneable)this.Tag).Clone() : this.Tag;
			return header;
		}

		public override string ToString()
		{
			return ("ColumnHeaderEx: Text: " + this.Text);
		}

		internal class InternalColInfo
		{
			public ContextMenuStrip DropDownContextMenu;
			public string Format;
			public IFormatProvider FormatProvider;
			public bool Hideable = true;
			public string NullValue;
			public bool ShowDropDownButton = false;
			public bool Sortable = true;
			public SortOrder SortOrder = SortOrder.None;
			public object Tag;
		}
	}
}